<?php
/*
 +----------------------------------------------------------------------
 + Title        : AddLog 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2022/2/11 21:46
 + Last-time    : 2022/2/11 21:46+ chen2
 + Desc         : AddLog
 +----------------------------------------------------------------------
*/


namespace app\http\middleware;

use app\admin\model\ActionLog;
use app\admin\model\AdminMenu;
use app\Request;
use think\facade\Log;
use think\Response;

class AddLog
{
    public function handle(Request $request, \Closure $next)
    {
        $response = $next($request);
        $this->addLog1($request, $response);
        $this->addLog($request, $response);
        return $response;
    }

    public function addLog1(\think\Request $request, Response $response)
    {
        //        try {
        $add['client_type'] = get_client_type();
        $add['user_id'] = get_client_id();
        $add['username'] = get_client_name();
        $add['app'] = app('http')->getName();
        $add['method'] = $request->method();
        $add['path'] = \request()->pathinfo();
        $add['router'] = sprintf('%s/%s/%s', app('http')->getName(), $request->controller(), $request->action());
        $add['module'] = get_client_name();
        $add['router_name'] = get_menu();
        $add['header'] = $request->header();
        $add['trans_id'] = $request->param('trans_id') ?? '';
        $add['query'] = $request->get();
        $add['params'] = $request->param();
        $add['result'] = getJson($response->getContent());
        $add['ip'] = get_client_ip();
        $add['status'] = $response->getCode();
        $add['create_time'] = date('Y-m-d H:i:s');
        ActionLog::build()
                 ->save($add);
        //        } catch (\Exception $e) {
        //            $res = $e->getMessage();
        //            \think\facade\Log::error('日志写入失败:' . $res);
        //        }
    }
    public function addLog(\think\Request $request, Response $response)
    {
        try {
            $add['客户端类型'] = get_client_type();
            $add['用户ID'] = get_client_id();
            $add['用户名称'] = get_client_name();
            $add['应用类型'] = app('http')->getName();
            $add['请求方式'] = $request->method();
            $add['菜单名称'] = get_menu();
            $add['请求路径'] = \request()->pathinfo();
            $add['请求路由'] = sprintf('%s/%s/%s', app('http')->getName(), $request->controller(), $request->action());
            $add['应用模块'] = get_client_name();
            $add['请求头'] = $request->header();
            $add['链路ID'] = $request->param('trans_id') ?? '';
            $add['query参数'] = $request->get();
            $add['请求参数'] = $request->param();
            $add['响应参数'] = getJson($response->getContent());
            $add['请求ID'] = get_client_ip();
            $add['响应状态'] = $response->getCode();
            $add['请求时间'] = date('Y-m-d H:i:s');
            Log::info(var_export($add, 1));
            ActionLog::build()
                     ->save($add);
        } catch (\Exception $e) {
            $res = $e->getMessage();
            \think\facade\Log::error('日志写入失败:' . $res);
        }
    }
}